package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;


import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.service.ISysTeacherService;
import com.ruoyi.web.controller.vo.StudentCountVoData;
import com.ruoyi.common.vo.StudentCountVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.service.ISysStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@Controller
@RequestMapping("/system/student")
public class SysStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private ISysStudentService sysStudentService;
    @Autowired
    private ISysTeacherService sysTeacherService;
    @RequiresPermissions("system:student:view")
    @GetMapping()
    public String student()
    {
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("system:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysStudent sysStudent)
    {
        startPage();
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        //TODO 实现通过老师id查到老师名字
        for (SysStudent sysStudent1 : list) {
            Long teacherId = sysStudent1.getTeacherId();
            //根据老师的编号查询老师
            SysTeacher sysTeacher = sysTeacherService.selectSysTeacherByTeacherId(Long.valueOf(teacherId));
            if(sysTeacher != null) {
                String teacherName = sysTeacher.getTeacherName();
                sysStudent1.setTeacherName(teacherName);
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("system:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysStudent sysStudent)
    {
        List<SysStudent> list = sysStudentService.selectSysStudentList(sysStudent);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.exportExcel(list, "学生数据");
    }

    /**
     * 新增学生
     */
    @RequiresPermissions("system:student:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("system:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysStudent sysStudent)
    {
        return toAjax(sysStudentService.insertSysStudent(sysStudent));
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("system:student:edit")
    @GetMapping("/edit/{stuId}")
    public String edit(@PathVariable("stuId") Long stuId, ModelMap mmap)
    {
        SysStudent sysStudent = sysStudentService.selectSysStudentByStuId(stuId);
        mmap.put("sysStudent", sysStudent);
        mmap.put("currentTeacherId",sysStudent.getTeacherId());
        return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("system:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysStudent sysStudent)
    {
        return toAjax(sysStudentService.updateSysStudent(sysStudent));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("system:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysStudentService.deleteSysStudentByStuIds(ids));
    }

    /**
     * 分组统计学生的人数:  获取老师下所有学生的数量
     * @return
     */
    @GetMapping("/getCountStudentGroupByTeacher")
    @ResponseBody
    public StudentCountVoData getCountStudentGroupByTeacher(){

        //从Service中的getStudentCountGroupByTeacher方法得到StudentCountVo类型的数据，存放到List中
        List<StudentCountVo> list = sysStudentService.getStudentCountGroupByTeacher();

        //用List接收老师的名字
        List<String> teacherNames=new ArrayList<>();
        //用List接收一个老师的总学生数量
        List<Integer> totals=new ArrayList<>();

        //核心代码：增强for遍历StudentCountVo中的数据
        for(StudentCountVo studentCountVo : list){
            teacherNames.add(studentCountVo.getTeacherName());
            totals.add(studentCountVo.getTotal());
        }

        StudentCountVoData studentCountVoData=new StudentCountVoData();
        studentCountVoData.setTotals(totals);
        studentCountVoData.setTeacherNames(teacherNames);

        return  studentCountVoData;
    }



}
